package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.api.stock.ErpUserAccountsServiceAPI;
import com.steel.yfmall.cloud.assistant.bean.param.APIParamUserAccounts;
import com.steel.yfmall.cloud.assistant.bean.result.APIResultErpUserAccounts;
import com.steel.yfmall.cloud.assistant.po.user.UserAccounts;
import com.steel.yfmall.cloud.assistant.po.user.UserAccountsExample;
import com.steel.yfmall.cloud.assistant.po.user.UserBase;
import com.steel.yfmall.cloud.assistant.po.user.UserBaseExample;
import com.steel.yfmall.cloud.assistant.user.mapper.UserAccountsMapper;
import com.steel.yfmall.cloud.assistant.user.mapper.UserBaseMapper;
import com.steel.yfmall.cloud.assistant.utils.HttpSendUtil;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: UserAccountsServiceImpl
 * @author: liuzhijun
 * @date: 2021-03-23 20:27
 **/
@Service
@Slf4j
public class ErpUserAccountsServiceAPIImpl implements ErpUserAccountsServiceAPI {

    @Value("${yf_erp_useraccounts_url}")
    private String YF_ERP_USERACCOUNTS_URL;

    @Resource
    private UserAccountsMapper userAccountsMapper;

    @Resource
    private UserBaseMapper userBaseMapper;

    @Override
    public APIMsgBean<APIResultErpUserAccounts> getUserAccountsMoneyInfo(APIParamUserAccounts apiParamUserAccounts) {
        APIMsgBean<APIResultErpUserAccounts> userAccountsAPIMsgBean = new APIMsgBean<>();
        userAccountsAPIMsgBean.setData(null);
        if (apiParamUserAccounts == null || apiParamUserAccounts.getId() == null || StringUtils.isBlank(apiParamUserAccounts.getUserName()) || StringUtils.isBlank(apiParamUserAccounts.getProductTypeNo())) {
            userAccountsAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return userAccountsAPIMsgBean;
        }

        String userName = apiParamUserAccounts.getUserName();
        Integer id = apiParamUserAccounts.getId();

        UserBaseExample userBaseExample = new UserBaseExample();
        userBaseExample.createCriteria().andUserNameEqualTo(userName);
        UserBase userBase = userBaseMapper.selectOneByExampleSelective(userBaseExample);
        if (userBase == null || StringUtils.isBlank(userBase.getOtherUserCode())) {
            userAccountsAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("查询不到erp客户编码"));
            return userAccountsAPIMsgBean;
        }


        UserAccountsExample accountsExample = new UserAccountsExample();
        accountsExample.createCriteria().andIdEqualTo(id).andUserNameEqualTo(userName);
        UserAccounts userAccounts = userAccountsMapper.selectOneByExampleSelective(accountsExample);
        if (userAccounts == null || StringUtils.isBlank(userAccounts.getAccountNumber()) || StringUtils.isBlank(userAccounts.getCompanyName())) {
            userAccountsAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("账户信息缺失！"));
            return userAccountsAPIMsgBean;
        }

        String accountNumber = userAccounts.getAccountNumber();
        String companyName = userAccounts.getCompanyName();
        String productTypeNo = apiParamUserAccounts.getProductTypeNo();
        String otherUserCode = userBase.getOtherUserCode();

        String abc = "_controller=sbjcApiForEC&_action=A&currency=CNY&compId=" + companyName +
                "&custNo=" + otherUserCode + "&aidItem=" + accountNumber + "&prodCode=" + productTypeNo;

        String result = "";
        try {
            log.info("查询用户账户信息url:" + YF_ERP_USERACCOUNTS_URL + ",参数:" + abc);
            result = HttpSendUtil.sendPost(YF_ERP_USERACCOUNTS_URL, abc, "");
        } catch (Exception e) {
            log.info("请求url异常:" + YF_ERP_USERACCOUNTS_URL);
        }

        APIResultErpUserAccounts apiResultUserAccounts = JSON.parseObject(result, APIResultErpUserAccounts.class);
        if (apiResultUserAccounts != null && "200".equals(apiResultUserAccounts.getRtnCode())) {
            userAccountsAPIMsgBean.setData(apiResultUserAccounts);
            userAccountsAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
            return userAccountsAPIMsgBean;
        } else {
            userAccountsAPIMsgBean.setData(null);
            userAccountsAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean(apiResultUserAccounts.getRtnMsg()));
            return userAccountsAPIMsgBean;
        }
    }


}
